<?php
require(dirname(dirname(__FILE__)) ."/php_sdk-master/Jpush/autoload.php");
use JPush\Client as JPush;
date_default_timezone_set("PRC");
/**
 * 极光消息推送服务 【送变电考勤系统】
 * 此消息推送使用别名指定推送
 * 推送到指定用户需要手机端设置用户别名 alias
 * 推送到相应平台需要手机端将用户平台设置为统一的tag 从而实现多平台发送
 * @copyright (c) 2018, gandianli
 * @author sunman
 */
class service_kqjpush extends gdl_components_baseservice
{
    # tang 极光平台配置参数 app与服务配置需一致
	//private static $appKey = "c9a63a1d618e84efc78a6e5f";
    //private static $secret = "5f9244476a04a8fe634ce97a";
	
	# tang 工程考勤
	//private static $appKey = "0e900577d450f5b96603be7d";
    //private static $secret = "cdb8689520c5f35e003d9ca0";
    # 公司帐号 极光平台配置参数 app与服务配置需一致
	private static $appKey = "7c73b76abb1ef04b7d11942a";
    private static $secret = "e381914e2557e36091966bbc";
	
    # 需要发送的手机系统 选择相应手机系统发送 默认发送所有
    public $systems = ['ios','android','windows'];
    # 极光分组id 目前默认200
    public $setId = '200';
    #默认推送所有平台 如果需要推送到相应平台请改值为数组['平台1','平台2'] 前提条件为app已经设置用户的tags为相应平台名
    public $company = 'all';
	#开发环境
	public $configArray = [
		'apns_production'=>false, #生产为true 开发为false
	];

    public function __construct($company="", $setId="",$systems="")
    {
        parent::__construct();
        # 初始化
        if(!empty($systems)){
            $this->systems = $systems;
        }
        if(!empty($setId)){
            $this->setId = $setId;
        }
        if(!empty($company)){
            $this->company = $company;
        }
    }

    /**
     * 统一推送消息 【通知栏展示】
     * @param $coentent 消息标题
     * @param $extras 推给app的必要参数 key/value形式['msgid'=>1,'type'=>'all']
     * @return msg
     */
    public function jpushAll($alert="", $extras="")
    {
        # 不带标题则不现实通知栏
		if(!empty($alert)){
			$content = $alert;
		}else{
			$content = "";
		}
        $JpushClient = new JPush(self::$appKey, self::$secret);
        $pusher = $JpushClient->push();
        $pusher->setPlatform($this->systems);
		$pusher->options($this->configArray);
        # 推送到全平台
        if($this->company=='all'){
            $pusher->addAllAudience();
        }
        # 推送到指定平台 一次最多允许20个
        if(is_array($this->company)){
            $pusher->addTag($this->company);
        }
        $pusher->addSegmentId($this->setId);
        # 多平台带参方式不一
        if(is_array($extras) && !empty($extras)){
            $pusher->iosNotification($content,$extras);
            $pusher->androidNotification($content,$extras);
            $pusher->addWinPhoneNotification($content,$content,'',$extras);
            $pusher->message($content,$extras);
        }else{
            # 不带参数 仅为一条消息
            $pusher->setNotificationAlert($content);
        }
	
        try {
            # 开始推送
            $resultData = $pusher->send();
            if($resultData['http_code']==200 || $resultData['http_code']=="200"){
                return true;
            }else{
                return false;
            }

        } catch (\JPush\Exceptions\JPushException $ex) {
            return $ex;
        }

        return false;
    }

    /**
     * 推送一条消息到指定用户上 【通知栏展示】
     * 前提：app端必须先设置每个用户别名 别名为用户identityId
     * @param $content string 消息标题
     * @param $alias array 用户别名 唯一标识 手机端必须先将用户identityId设置成别名后才可用 ['55','66','77']
     * @param $extras array 推给app的必要参数 key/value形式['msgid'=>1,'type'=>'all'] msgid 为消息id， type=weigui 考试违规 type=all 正常消息 type=kaoshi 考试提醒 参数可以自定义key/value形式都可以
     * @return msg
     */
    public function jpushOneMessage($alert="", $alias, $extras){
		
		# 不带标题则不现实通知栏
        if(!empty($alert)){
			$content = $alert;
		}else{
			$content = "";
		}
        if(!is_array($alias)){
            echo '请选择需要推送的用户';
            return false;
        }
        $JpushClient = new JPush(self::$appKey, self::$secret);
		
        $pusher = $JpushClient->push();
		$pusher->options($this->configArray);
        $pusher->setPlatform($this->systems);
        $pusher->addAlias($alias); # 指定用户
        $pusher->addSegmentId($this->setId);
        # 多平台带参方式不一
        if(is_array($extras) && !empty($extras)){
			//通知栏消息
            $pusher->iosNotification($content,$extras);
            $pusher->androidNotification($content,$extras);
            $pusher->addWinPhoneNotification($content,$content,'',$extras);
			$pusher->setNotificationAlert($content);
			
			// 自定义消息
            //$pusher->message($content,$extras);
        }else{
            # 不带参数 仅为一条消息
            //$pusher->setNotificationAlert($content);
        }

        try {
            # 开始推送
            $resultData = $pusher->send();
            if($resultData['http_code']==200 || $resultData['http_code']=="200"){
                return true;
            }else{
                return false;
            }

        } catch (\JPush\Exceptions\JPushException $ex) {
            return $ex;
        }

        return false;
    }

    /**
     * 自定义消息
     * 推送一条消息到指定用户上 【通知栏展示】
     * 前提：app端必须先设置每个用户别名 别名为用户identityId
     * @param $content string 消息标题
     * @param $alias array 用户别名 唯一标识 手机端必须先将用户identityId设置成别名后才可用 ['55','66','77']
     * @param $extras array 推给app的必要参数 key/value形式['msgid'=>1,'type'=>'all'] msgid 为消息id， type=weigui 考试违规 type=all 正常消息 type=kaoshi 考试提醒 参数可以自定义key/value形式都可以
     * @return msg
     */
    public function jpushOneMsg($alert="", $alias, $extras){

        # 不带标题则不现实通知栏
        if(!empty($alert)){
            $content = $alert;
        }else{
            $content = "";
        }
        if(!is_array($alias)){
            echo '请选择需要推送的用户';
            return false;
        }
        $JpushClient = new JPush(self::$appKey, self::$secret);

        $pusher = $JpushClient->push();
        $pusher->setPlatform($this->systems);
        $pusher->addAlias($alias); # 指定用户
        $pusher->addSegmentId($this->setId);
        $pusher->options($this->configArray);
        # 多平台带参方式不一
        if(is_array($extras) && !empty($extras)){
            //通知栏消息
//            $pusher->iosNotification($content,$extras);
//            $pusher->androidNotification($content,$extras);
//            $pusher->addWinPhoneNotification($content,$content,'',$extras);
//            $pusher->setNotificationAlert($content);

            // 自定义消息
            $pusher->message($content,$extras);
        }else{
            # 不带参数 仅为一条消息
            $pusher->setNotificationAlert($content);
        }

        try {
            # 开始推送
            $resultData = $pusher->send();
            if($resultData['http_code']==200 || $resultData['http_code']=="200"){
                return true;
            }else{
                return false;
            }

        } catch (\JPush\Exceptions\JPushException $ex) {
            return $ex;
        }

        return false;
    }
    /**
     * 定时推送
     * @param $name
     * @param $push_payload
     * @param $trigger array(
    "start"=>"2018-08-01 12:00:00",
    "end"=>"2099-12-25 12:00:00",
    "time"=>"14:00:00",
    "time_unit"=>"DAY",
    "frequency"=>1
    )
     * @param $alias
     * @return mixed
     */
    public function jpushTimedTaskMessage($name,$alert,$extras, $trigger,$alias,$type,$schedule_id){
        # 不带标题则不现实通知栏
        if(!empty($alert)){
            $content = $alert;
        }else{
            $content = "";
        }

        if(!is_array($alias)){
            echo '请选择需要推送的用户';
            return false;
        }
        $JpushClient = new JPush(self::$appKey, self::$secret);

        $schedule = $JpushClient->schedule();
//        $ss = $schedule->getSchedules($page=100);
//        print_r($ss);die;
        $payload = $JpushClient->push()
            ->setPlatform($this->systems)
//            ->addAllAudience()
            ->iosNotification($content,$extras)
            ->androidNotification($content,$extras)
//            ->addWinPhoneNotification($content,$extras)
            ->setNotificationAlert($content)
            ->message($content,$extras)
            ->options($this->configArray)
            ->addAlias($alias)
            ->addSegmentId($this->setId)
            ->build();

        try {
//            $rr = $schedule->getSchedules($page=1);
//            foreach ($rr['body']['schedules'] as $ooo){
//                $aaa= $schedule->deleteSchedule($ooo['schedule_id']);
//            }
//            die;
//            $schedule->deleteSchedule($schedule_id);
            if ($type==2){
                # 更新定时任务
//                $resultData = $schedule->updateSingleSchedule($schedule_id, $name, null, $payload, $trigger);
                $resultData = $schedule->updatePeriodicalSchedule($schedule_id, $name, null, $payload, $trigger);
                
            }else{
                //创建定时任务
                $resultData = $schedule->createPeriodicalSchedule($name, $payload, $trigger);
                
            }
            if($resultData['http_code']==200 || $resultData['http_code']=="200"){
                return $resultData;
            }else{
                return false;
            }

        } catch (\JPush\Exceptions\JPushException $ex) {
            return $ex;
        }

    }
	
	/*
	 别名注销和重新生成
	*/
	public function setUserAlias($userId=""){
		if(empty($userId)){return false;}
		try{
			$JpushClient = new JPush(self::$appKey, self::$secret);
			$pusher = $JpushClient->Device(); 
			#删除别名 alis
			$ret = $pusher->deleteAlias($userId);
			return true;
		}catch(Exceptions $ex){
			return false;
		}
		return false;
	}

}